/**
 * Created by vincent on 2017/9/14.
 */
moduleStudyMap.controller("StudyClassGroupController", function ($scope, $state, $log, $timeout, LoadingService, DialogService,
                                                                 ConstantService, EnumService, StudyClassGroupResource,
                                                                 StudyClassResource, TaskStudyResource, StudyMapBadgeRuleResource
    , StudyMapEventRuleResource) {

    $scope.$on("$viewContentLoaded", function () {
        ConstantService.addState($state.current);
        getStudyClassGroupList();
        getStudyClassList();
        getTaskStudyClass();
        $scope.expandPanel("GROUP_VIEW");
    });

    $scope.currentView = {key: "GROUP_VIEW"};
    $scope.studyClassOptions = Enums.StudyClassTypeEnum.options;

    $scope.expandPanel = function (key) {
        if (!StringUtils.isNlOrUndOrEmpty(key)) {
            $scope.currentView.key = key;
            pageOnLoad();
        }
    };

    var getStudyClassGroupList = function () {
        //LoadingService.show();
        $scope.currentStudyMapId = ConstantService.getItem("currentStudyMapId");
        StudyClassGroupResource.findListByStudyMapId({studyMapId: $scope.currentStudyMapId}, function (groupResult) {
            $log.debug("学习地图下章节列表", groupResult);
            $scope.groupList = groupResult;
            //LoadingService.hide();
        });
    };
    $scope.getStudyClassGroupList = getStudyClassGroupList;

    $scope.addClassGroup = function () {
        DialogService.open("addStudyClassGroup", $scope);
    };

    $scope.refreshClassGroup = function () {
        getStudyClassGroupList();
    };

    $scope.editClassGroup = function (group) {
        $scope.currentClassGroup = group;
        DialogService.open("editStudyClassGroup", $scope);
    };

    $scope.setGroupValid = function (group) {
        Alert.confirm("确认要将该章节设为有效吗", function () {
            //LoadingService.show();
            StudyClassGroupResource.setValid(group, function (groupResult) {
                $log.debug("设为有效结果", groupResult);
                angular.copy(groupResult, group);
                $timeout(function () {
                    //LoadingService.hide();
                    Alert.success("该章节已设为有效！");
                }, 300);
            }, function () {
                //LoadingService.hide();
            });
        });
    };

    $scope.setGroupInvalid = function (group) {
        Alert.confirm("确认要将该章节设为无效吗", function () {
            //LoadingService.show();
            StudyClassGroupResource.setInvalid(group, function (groupResult) {
                $log.debug("设为无效结果", groupResult);
                angular.copy(groupResult, group);
                $timeout(function () {
                    //LoadingService.hide();
                    Alert.success("该章节已设为无效！");
                }, 300);
            }, function () {
                //LoadingService.hide();
            });
        });
    };

    var getStudyClassList = function () {
        $scope.currentStudyClassGroupId = ConstantService.getItem("currentStudyClassGroupId");
        if (!StringUtils.isNlOrUndOrEmpty($scope.currentStudyClassGroupId)) {
            //LoadingService.show();
            StudyClassResource.findListByGroupId({groupId: $scope.currentStudyClassGroupId}, function (classResult) {
                $log.debug("章节下课程列表", classResult);
                $scope.classList = classResult;
                //LoadingService.hide();
            }, function () {
                //LoadingService.hide();
            });
        }
    };

    $scope.setupStudyClass = function (group) {
        ConstantService.setItem("currentStudyClassGroupId", group.id);
        getStudyClassList();
        $scope.expandPanel("CLASS_VIEW");
    };

    $scope.setupEventURL = function (group) {
        //LoadingService.show();
        $scope.currentGroup = group;
        StudyMapEventRuleResource.findByStudyClassGroupIdEventType({studyClassGroupId: group.id, eventType: 'URL'}, function (eventRuleResult) {
            //$log.debug("章节活动结果", eventRuleResult);
            $scope.currentGroupEventRule = eventRuleResult;
            $scope.eventType = 'URL';
            DialogService.open("setupEvent", $scope);
            //LoadingService.hide();
        }, function () {
            //LoadingService.hide();
        });
    };

    $scope.setupEventStudyMap = function (group) {
        //LoadingService.show();
        $scope.currentGroup = group;
        StudyMapEventRuleResource.findByStudyClassGroupIdEventType({studyClassGroupId: group.id, eventType: 'STUDY_MAP'}, function (eventRuleResult) {
            //$log.debug("章节活动结果", eventRuleResult);
            $scope.currentGroupEventRule = eventRuleResult;
            $scope.eventType = 'STUDY_MAP';
            DialogService.open("setupEvent", $scope);
            //LoadingService.hide();
        }, function () {
            //LoadingService.hide();
        });
    }

    $scope.setupBadge = function (group) {
        //LoadingService.show();
        $scope.currentGroup = group;
        StudyMapBadgeRuleResource.findByStudyClassGroupId({studyClassGroupId: group.id}, function (badgeRuleResult) {
            $log.debug("章节勋章结果", badgeRuleResult);
            $scope.currentGroupBadgeRule = badgeRuleResult;
            DialogService.open("setupBadge", $scope);
            //LoadingService.hide();
        }, function () {
            //LoadingService.hide();
        });
    };

    $scope.addStudyClass = function () {
        DialogService.open("addStudyClass", $scope);
    };

    $scope.refreshStudyClass = function () {
        getStudyClassList();
    };

    $scope.editStudyClass = function (studyClass) {
        $scope.currentStudyClass = studyClass;
        $scope.editingStudyClass = angular.copy($scope.currentStudyClass);
        $scope.editingStudyClass.typeOption = EnumService.getEnumObject($scope.editingStudyClass.type, "StudyClassTypeEnum");
        DialogService.open("editStudyClass", $scope);
    };

    $scope.setClassValid = function (studyClass) {
        Alert.confirm("确认要将该课程设为有效吗", function () {
            //LoadingService.show();
            StudyClassResource.setValid(studyClass, function (classResult) {
                $log.debug("设为有效结果", classResult);
                angular.copy(classResult, studyClass);
                $timeout(function () {
                    //LoadingService.hide();
                    Alert.success("该章节已设为有效！");
                }, 300);
            }, function () {
                //LoadingService.hide();
            });
        });
    };

    $scope.setClassInvalid = function (studyClass) {
        Alert.confirm("确认要将该课程设为无效吗", function () {
            //LoadingService.show();
            StudyClassResource.setInvalid(studyClass, function (classResult) {
                $log.debug("设为无效结果", classResult);
                angular.copy(classResult, studyClass);
                $timeout(function () {
                    //LoadingService.hide();
                    Alert.success("该章节已设为无效！");
                }, 300);
            }, function () {
                //LoadingService.hide();
            });
        });
    };

    $scope.setGauge = function (studyClass) {
        $scope.currentStudyClass = studyClass;
        DialogService.open("setExaminationTipsStudyClass", $scope);
    };

    $scope.setExamination = function (studyClass) {
        $scope.currentStudyClass = studyClass;
        DialogService.open("setExaminationStudyClass", $scope);
    };

    $scope.setExaminationTips = function (studyClass) {
        $scope.currentStudyClass = studyClass;
        DialogService.open("setExaminationTipsStudyClass", $scope);
    };

    $scope.setH5 = function (studyClass) {
        $scope.currentStudyClass = studyClass;
        DialogService.open("setH5StudyClass", $scope);
    };

    $scope.setMicroH5 = function (studyClass) {
        $scope.currentStudyClass = studyClass;
        DialogService.open("setMicroH5StudyClass", $scope);
    };

    var getTaskStudyClass = function () {
        $scope.currentStudyClassId = ConstantService.getItem("currentStudyClassId");
        if (!StringUtils.isNlOrUndOrEmpty($scope.currentStudyClassId)) {
            //LoadingService.show();
            StudyClassResource.getTarget({studyClassId: $scope.currentStudyClassId}, function (taskClassResult) {
                $log.debug("课程相关任务信息", taskClassResult);
                $scope.currentTaskStudyClass = taskClassResult;
                //LoadingService.hide();
            }, function () {
                //LoadingService.hide();
            });
        }
    };

    $scope.setTask = function (studyClass) {
        $scope.currentStudyClass = studyClass;
        ConstantService.setItem("currentStudyClassId", studyClass.id);
        getTaskStudyClass();
        $scope.expandPanel("TASK_VIEW");
    };

    $scope.addTaskStudy = function () {
        DialogService.open("addTaskStudy", $scope);
    };

    $scope.refreshTaskStudy = function () {
        getTaskStudyClass();
    };

    $scope.editTaskStudy = function (taskStudy) {
        $scope.currentTaskStudy = taskStudy;
        $scope.editingTaskStudy = angular.copy($scope.currentTaskStudy);
        DialogService.open("editTaskStudy", $scope);
    };

    $scope.setTaskValid = function (task) {
        Alert.confirm("确认要将该任务设为有效吗", function () {
            //LoadingService.show();
            TaskStudyResource.setValid(task, function (taskResult) {
                $log.debug("设为有效结果", taskResult);
                angular.copy(taskResult, task);
                $timeout(function () {
                    //LoadingService.hide();
                    Alert.success("该任务已设为有效！");
                }, 300);
            }, function () {
                //LoadingService.hide();
            });
        });
    };

    $scope.setTaskInvalid = function (task) {
        Alert.confirm("确认要将该任务设为无效吗", function () {
            //LoadingService.show();

            var bean = {
                id: task.id,
                studyClassId: $scope.currentStudyClassId
            }

            TaskStudyResource.setInvalid(bean, function (taskResult) {
                $log.debug("设为无效结果", taskResult);
                angular.copy(taskResult, task);
                $timeout(function () {
                    //LoadingService.hide();
                    Alert.success("该任务已设为无效！");
                }, 300);
            }, function () {
                //LoadingService.hide();
            });
        });
    };

    $scope.setupQuestionnaire = function (group) {
        $scope.currentGroup = group;
        DialogService.open("setupQuestionnaire", $scope);
    }

    $scope.publish = function (group) {
        Alert.confirm("是否确认要执行该操作？", function () {
            //LoadingService.show();
            StudyClassGroupResource.publish({}, {studyClassGroupId: group.id}, function () {
                //$log.debug("勋章匹配结果",badgeResult);
                //$scope.badgeResult = badgeResult;
                //LoadingService.hide();
                Alert.success("操作成功！");
                //DialogService.close();
            }, function () {
                //LoadingService.hide();
            });
        });


        //
    }
});